package com.gjxx.netty.runnable;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gjxx.buss.domain.TWarning;
import com.gjxx.buss.service.ITWarningService;
import com.gjxx.common.enums.WarningTypeEnum;
import com.gjxx.common.tcp.LocationInfoUploadMsg;
import com.gjxx.common.utils.SpringBeanUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 */
@Slf4j
//@Component
public class WarningRunnable implements Runnable {
    /**
     * 消息文本
     */
    private String message;

    private ITWarningService itWarningService;

    public WarningRunnable(String message) {
        this.message = message;
        this.itWarningService = SpringBeanUtil.getBean(ITWarningService.class);
    }

    @Override
    public void run() {
        LocationInfoUploadMsg locationInfoUploadMsg = JSON.parseObject(message, LocationInfoUploadMsg.class);
        try {
            String warningFlagField = locationInfoUploadMsg.getWarningFlagField();
            if (warningFlagField == null || warningFlagField.length() < 32) {
                log.warn("[报警信息消息队列消费] warningFlagField 格式不合法: {}", warningFlagField);
                return;
            }

            // 构建索引与警告类型的映射关系
            int[] indices = {
                    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18, 19,
                    20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
            };
            WarningTypeEnum[] warningTypes = {
                    WarningTypeEnum.thirtytwo, WarningTypeEnum.thirtyone, WarningTypeEnum.thirty,
                    WarningTypeEnum.twentynine, WarningTypeEnum.twentyeight, WarningTypeEnum.twentyseven,
                    WarningTypeEnum.twentysix, WarningTypeEnum.twentyfive, WarningTypeEnum.twentyfour,
                    WarningTypeEnum.twentythree, WarningTypeEnum.twentytwo, WarningTypeEnum.twentyone,
                    WarningTypeEnum.twenty, WarningTypeEnum.nineteen, WarningTypeEnum.fifteen,
                    WarningTypeEnum.fourteen, WarningTypeEnum.thirteen, WarningTypeEnum.twelve,
                    WarningTypeEnum.eleven, WarningTypeEnum.ten, WarningTypeEnum.nine,
                    WarningTypeEnum.eight, WarningTypeEnum.seven, WarningTypeEnum.six,
                    WarningTypeEnum.five, WarningTypeEnum.four, WarningTypeEnum.three,
                    WarningTypeEnum.two, WarningTypeEnum.one
            };

            for (int i = 0; i < indices.length; i++) {
                int index = indices[i];
                if ("1".equals(String.valueOf(warningFlagField.charAt(index)))) {
                    String warningType = warningTypes[i].getCode();
                    extracted(locationInfoUploadMsg, warningType);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[报警信息消息队列消费] 保存报警信息失败:{}",e.getMessage());
        }

    }

    private void extracted(LocationInfoUploadMsg locationInfoUploadMsg, String warningType) {
        String simNo = locationInfoUploadMsg.getMsgHeader().getTerminalPhone();
        if(warningType != null){
            List<String> handleStatusList = new ArrayList<>();
            handleStatusList.add("01");
            handleStatusList.add("03");
            List<TWarning> warningList = itWarningService.list(new QueryWrapper<TWarning>()
                    .eq("sim_no",simNo).eq("warning_type", warningType)
                    .in("handle_status", handleStatusList));
            log.info("warningType:{}", warningType);
            log.info("simNo:{}",simNo);
            log.info("报警信息:{}",warningList.size());
            if(warningList.size() > 0){
                TWarning warning = warningList.get(0);
                warning.setSimNo(simNo);
                warning.setWarningCount(warning.getWarningCount()+1);
                warning.setUpdateTime(new Date());
                itWarningService.updateById(warning);
            }else {
                TWarning tWarning = new TWarning();
                tWarning.setSimNo(simNo);
                tWarning.setWarningType(warningType);
                tWarning.setWarningCount(1L);
                tWarning.setHandleStatus("01");
                itWarningService.save(tWarning);
            }
        }
    }
}
